package com.wo.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wo.cloud.admin.entity.SysNotify;
import com.wo.cloud.admin.entity.SysUserNotify;
import com.wo.cloud.admin.service.ISysNotifyService;
import com.wo.cloud.admin.service.ISysUserNotifyService;
import com.wo.cloud.admin.util.SysFeignConstant;
import com.wo.cloud.core.entity.Response;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/5/15 22:43
 * Description: 通知controller
 */

@RestController
@RequestMapping(SysFeignConstant.SYS_NOTIFY_BASE_URL)
@Api(tags = "系统通知模块")
public class SysNotifyController {

    @Autowired
    private ISysNotifyService sysNotifyService;

    @Autowired
    private ISysUserNotifyService userNotifyService;

    // 用户获取所有系统通知消息
    @GetMapping("/user/allNotify")
    public Response getSysNotify() {
        return sysNotifyService.getAllSysNotify();
    }

    // 用户获取未读通知
    @GetMapping("/user/unreadNotify")
    public Response getUnReadNotify() {
        return sysNotifyService.getUnreadNotify();
    }

    // 新增消息
    @PostMapping("/save")
    public Response<Object> save(@RequestBody SysNotify sysNotify) {
        boolean result = sysNotifyService.saveOrUpdate(sysNotify);
        if (!result) {
            return Response.fail();
        }
        return Response.data(sysNotify.getId());
    }

    // 删除消息
    @DeleteMapping("/{id}")
    public Response<Object> delete(@PathVariable("id") String id) {
        boolean removeById = sysNotifyService.removeById(id);
        if (!removeById) {
            return Response.fail();
        }
        return Response.data(id);
    }

    // 消息列表
    @GetMapping("/list")
    public Response<List<SysNotify>> list() {
        List<SysNotify> list = sysNotifyService.list();
        return Response.data(list);
    }

    // 消息分页
    @GetMapping("/page")
    public Response page(Page page) {
        IPage<SysNotify> sysNotifyPage = sysNotifyService.getSysNotifyPage(page);
        return Response.data(sysNotifyPage);
    }

    // 消息已经阅读，修改阅读状态
    @GetMapping("/updateStatus")
    public Response updateStatus(@RequestParam(value = "id") String userNotifyId) {
        SysUserNotify sysUserNotify = userNotifyService.updateStatus(userNotifyId);
        return Response.data(sysUserNotify.getId());
    }

}
